package com.vip.mongofile.util;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.context.ApplicationContext;

import java.util.Map;

/**
 * @Description: SpringBeanUtils.
 * @Date: 2020/12/17 15:38
 * @Author: wp
 **/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class SpringBeanUtils {

    private static final SpringBeanUtils INSTANCE = new SpringBeanUtils();

    private ApplicationContext cfgContext;

    /**
     * get SpringBeanUtils.
     *
     * @return SpringBeanUtils
     */
    public static SpringBeanUtils getInstance() {
        return INSTANCE;
    }

    /**
     * acquire spring bean.
     *
     * @param type type
     * @param <T>  class
     * @return bean
     */
    public <T> T getBean(final Class<T> type) {
        return cfgContext.getBean(type);
    }

    /**
     * acquire spring bean.
     *
     * @param type type
     * @param <T>  class
     * @return  Map<beanName,bean>
     */
    public <T> Map<String,T> getBeansOfType(final Class<T> type){
        return cfgContext.getBeansOfType(type);
    }

    /**
     * set application context.
     *
     * @param cfgContext application context
     */
    public void setCfgContext(final ApplicationContext cfgContext) {
        this.cfgContext = cfgContext;
    }
}
